namespace Backoffice.Domain.Entities.App
{
    /// <summary>
    /// 菜单管理功能
    /// </summary>
    public class AppMenu : EntityBase
    {
        public string Name { get; private set; }
        public string? Url { get; private set; }
        public Guid? ParentId { get; private set; }
        public int Order { get; private set; }

        public virtual List<AppMenu> Children { get; private set; } = new();

        public AppMenu(string name, string? url = null, Guid? parentId = null, int order = 0)
        {
            Name = name;
            Url = url;
            ParentId = parentId;
            Order = order;
        }

        public void UpdateInfo(string name, string? url, int order)
        {
            Name = name;
            Url = url;
            Order = order;
        }
        public void UpdateParentAndOrder(Guid? parentId, int order)
        {
            ParentId = parentId;
            Order = order;
        }

        public void AddChild(AppMenu child)
        {
            if (!Children.Any(c => c.Id == child.Id))
            {
                Children.Add(child);
            }
        }

        public void RemoveChild(Guid childId)
        {
            var child = Children.FirstOrDefault(c => c.Id == childId);
            if (child != null)
            {
                Children.Remove(child);
            }
        }
        public AppMenu Clone()
        {
            return new AppMenu(Name, Url, ParentId, Order)
            {
                Id = Id,
                Children = Children.Select(c => c.Clone()).ToList()
            };
        }
    }
}